﻿using Gl_mes.BLL.Mes;
using MesGLApp.DAL.MesDB;
using MesGLApp.DAL.MesDTO;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace MesGLApp.View.Search
{
    partial class ProdInfoFrm : Smobiler.Core.Controls.MobileForm
    {
        #region "内部变量"
        private ProdProcessControler _ctrl = new ProdProcessControler();
        private ProdCycleControler _cycle_ctrl = new ProdCycleControler();
        private List<SiteInfo> _site_list = new List<SiteInfo>();
        private List<SiteInfo> _parent_list = new List<SiteInfo>();
        private List<StatusInfo> _status_list = new List<StatusInfo>();

        TargetProdProcessDTO _curr_tar = new TargetProdProcessDTO();

        private string _prodNo = "";
        #endregion

        public ProdInfoFrm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }

        public ProdInfoFrm(string prodNo) : base()
        {
            InitializeComponent();
            this._prodNo = prodNo;
        }

        #region "控件事件"
        /// <summary>
        /// 关闭本页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Press(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region "内部方法"
        private void InitControlValue(TargetProdProcessDTO tarBean)
        {
            if (tarBean != null)
            {
                this.lblProdNumber.Text = tarBean.ProdNumber;
                this.lblMto.Text = tarBean.MtoNumber;
                this.lblMo.Text = tarBean.MoNumber;
                this.lblClientCode.Text = tarBean.ClientCode;
                this.lblSalers.Text = tarBean.SalerName;
                this.lblMatNumber.Text = tarBean.GoodsNumber;
                this.lblMatName.Text = tarBean.GoodsName;
                this.lblMatSpe.Text = tarBean.GoodsSpe;
                this.lblGoodsLength.Text = Math.Round(tarBean.GoodsLength ?? 0, 2).ToString();
                this.lblOrderQty.Text = Math.Round(tarBean.OrderQty ?? 0, 2).ToString();

                if (this._status_list != null && this._status_list.Count > 0)
                {
                    var query1 = this._status_list.Where(s => s.ID == tarBean.ProdStatusID);
                    if (query1 != null && query1.Count() > 0)
                    {
                        this.lblProdStatus.Text = query1.First().StatusValue;
                    }

                    var query2 = this._status_list.Where(s => s.ID == tarBean.UrgentStatusID);
                    if (query2 != null && query2.Count() > 0)
                    {
                        this.lblUrgentStatus.Text = query2.First().StatusValue;
                        if (query2.First().StatusValue.Contains("红单"))
                        {
                            this.lblUrgentStatus.BackColor = Color.Red;
                        }
                        else if (query2.First().StatusValue.Contains("黄单"))
                        {
                            this.lblUrgentStatus.BackColor = Color.Yellow;
                        }
                        else
                        {
                            this.lblUrgentStatus.BackColor = Color.White;
                        }
                    }
                }

                if (this._site_list != null && this._parent_list != null)
                {
                    var query = from a in this._site_list
                                join b in this._parent_list on a.ParentID equals b.ID into res
                                from ab in res
                                where a.ID == tarBean.CurrSiteID
                                select new {A=a,ab=ab};
                    if (query != null && query.Count() > 0)
                    {
                        this.lblCurrSite.Text = query.First().ab.SiteName + "===>" + query.First().A.SiteName;
                    }
                }
                this.lblProdQty.Text = Math.Round(tarBean.ProdQty ?? 0, 2).ToString();

                this.lblClearQty.Text = "0";
                if (tarBean.scanList != null && tarBean.scanList.Count > 0)
                {
                    decimal clearQty = tarBean.scanList.Where(p => p.SiteInfoID == 20).Sum(p => p.ScanQty);
                    this.lblClearQty.Text = Math.Round(clearQty, 2).ToString();
                }

                SourcePPCycle sourceBean = this._cycle_ctrl.SearchSourcePPCycleForProdNo(tarBean.ProdNumber.Split('|')[0].ToString());
                if (sourceBean != null && sourceBean.PlanDate.HasValue)
                {
                    this.lblPlanDate.Text = sourceBean.PlanDate.Value.ToString("yyyy-MM-dd");
                }
            }
            else
            {
                
            }
        }
        #endregion

        private void ProdInfoFrm_Load(object sender, EventArgs e)
        {
            this._site_list = this._ctrl.SearchAllSiteInfo();
            this._parent_list = this._ctrl.SearchAllParentSiteInfo();
            this._status_list = this._ctrl.SearchAllStatus();
            if (this._prodNo != null && this._prodNo.Trim().Length > 0)
            {
                List<TargetProdProcessDTO> tmpList = this._ctrl.SearchTargetProdForCondition(this._prodNo, "", "", null);
                if (tmpList != null && tmpList.Count > 0)
                {
                    this._curr_tar = tmpList[0];
                    this.InitControlValue(this._curr_tar);
                }
                else
                {
                    MessageBox.Show("无法找到工单" + this._prodNo + "的资料！");
                }
            }
            else
            {
                MessageBox.Show("工单号不能为空！");
            }
        }

        
    }
}